package com.supermap.application.controller;

import com.alibaba.fastjson.JSONObject;
import com.chinacreator.asp.comp.sys.oauth2.sso.client.spi.SysMgrOAuth2SSOFilterSpiImpl;
import com.supermap.application.common.IndexInfoVO;
import com.supermap.application.common.OpStatus;
import com.supermap.application.common.UserFromKC;
import com.supermap.application.common.UserInfo;
import com.supermap.application.entity.User;
import com.supermap.application.service.UserService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by sun on 2018/2/15.
 * User控制层
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * spring boot 自带的 application.properties 配置文件值获取方法
     */
    @Value("${com.supermap.tj}")
    private String properties;

    @RequestMapping(value = "/test")
    @ResponseBody
    public User test(@RequestParam(value = "name")String name){
        User user = userService.findUserByName(name);
        if(null != user){
            System.out.println(properties);
            return user;
        }else{
            return null;
        }
    }


    @RequestMapping(value = "/testJson")
    @ResponseBody
    public String testJson(@RequestBody IndexInfoVO datas){
        System.out.println(datas);
        return datas.toString();
    }

    @RequestMapping(value = "/testStatic")
    @ResponseBody
    public void test(){
        Map<String,Object> map = new HashMap<>();
        UserFromKC.setNAME("admin");
        UserFromKC.setPASSWORD("admin");
    }

    /**
     * 获取登录信息，前端调用实现自动登录
     * @return
     */
    @RequestMapping(value = "/getLoginInfo")
    @ResponseBody
    public Map<String,Object> getName(){
        Map<String,Object> map = new HashMap<>();
        String name = UserFromKC.getNAME();
        System.out.println("getLoginInfo ======> 获得登录的用户名：" + name);
        if (name == null || name.equals("")){
            map.put("name","");
            map.put("password","");
        }else {
            User userByName = this.userService.findUserByName(name);
            map.put("name",userByName.getUserName());
            map.put("password",userByName.getPassword());
        }
        return map;
    }


    /**
     *退出登录时清空用户名密码
     */
    @CrossOrigin
    @RequestMapping(value = "/setLogOut")
    @ResponseBody
    public OpStatus setLogOut(){
        System.out.println("退出系统前用户名：" + UserFromKC.getNAME());
        UserFromKC.setNAME("");
        UserFromKC.setPASSWORD("");
        System.out.println("退出系统后用户名：" + UserFromKC.getNAME());
        return new OpStatus(true,"用户登录信息已清除！",null);
    }


    /**
     * 接收科创用户信息，向SGIS发送
     * @param userInfo
     * @return
     */
    @RequestMapping(value = "/control")
    @ResponseBody
    public JSONObject controll(@RequestBody UserInfo userInfo){
        System.out.println(userInfo);
        JSONObject jsonObject = null;
        String operation = userInfo.getOperation();
        if (operation.equals("add") || operation.equals("update")){
            jsonObject = this.userService.updataUserToSGIS(userInfo);
        }else {
            jsonObject = this.userService.deleteUserInSGIS(userInfo);
        }
        return jsonObject;
    }
}